﻿using System;
using System.Collections.Generic;
using System.Linq;
using Business.DbConnect;

namespace Business
{
    public class CustomerBusiness
    {
        public static bool Insert(Customer customer)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                entities.Customers.Add(customer);
                entities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static List<Customer> Select()
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                return entities.Customers.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Customer Select(Int64 id)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                return entities.Customers.FirstOrDefault(ob => ob.Id == id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Int64 SelectIdbyName(string name)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var query = from cs in entities.Customers
                            where name.ToLower().Trim() == cs.FullName.ToLower().Trim()
                            select cs.Id;
                return query.First();
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static bool Update(Customer customer)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var customers = entities.Customers.Find(customer.Id);
                entities.Entry(customers).CurrentValues.SetValues(customer);
                entities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static List<Customer> SearchCustomerByText(string txtInput)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                return entities.Customers.Where(ob => ob.FullName.Contains(txtInput)).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static IQueryable<String> Suggestion(string searchText)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var query = from cs in entities.Customers
                            select cs.FullName;
                var namelist = query.Where(n => n.ToLower().StartsWith(searchText.ToLower()));
                return namelist;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}